#!/bin/sh
#RAN=$(openssl rand -hex 16)
#echo -n "你的数据" | openssl dgst -sha512 -hmac "_sF~#_Nx3'6Mrp)"
#(stdin)= f2c922ddd2c7e248a40712989


# 配置参数
PRODUCT_ID="1234"
SN="1234567890"
SECRET_KEY="_sF~#_Nx3'6Mrp)"  # 替换为实际密钥
HEADER='{"alg":"HS512","typ":"JWT"}'  # 算法指定为HS512
RAN=$(openssl rand -hex 16)
PAYLOAD='{"pid":"'$PRODUCT_ID'", "sub":"'$SN'","iat":'$(date +%s)'000,"exp":'$(($(date +%s) + 300))'000,"code":"'$RAN'"}'  # 示例payload含过期时间

echo $PAYLOAD

# Base64Url编码函数
base64url() {
    openssl base64 | tr -d '=' | tr '+/' '-_' | tr -d '\n'
}

# 生成JWT各部分
jwt_hs512() {
    ENCODED_HEADER=$(echo "$HEADER" | base64url)
    ENCODED_PAYLOAD=$(echo  "$PAYLOAD" | base64url)
    SIGNATURE=$(echo "${ENCODED_HEADER}.${ENCODED_PAYLOAD}" | openssl dgst -sha512 -hmac "$SECRET_KEY" -binary | base64url)
    JWT="${ENCODED_HEADER}.${ENCODED_PAYLOAD}.${SIGNATURE}"
    echo $JWT
}
echo $(jwt_hs512 )
